﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace Transportr
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.Sample.IsDataLoaded)
            {
                App.ViewModel.Sample.BeginUpdate(new Transportr.Metadata.SimpleFilter());
                App.ViewModel.FavoriteLines.BeginUpdate();
            }
            MainPage_UpdateBrowser(this, e);
        }

        private void MainPage_UpdateBrowser(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.Browser.IsDataLoaded)
            {
                App.ViewModel.Browser.BeginUpdate();
            }
        }

        private void appbarAboutItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/About.xaml", UriKind.Relative));
        }

        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            string tempUri;
            if ((e.OriginalSource as Button).Tag != null)
            {
                tempUri = String.Format("/Pages/LineDetails.xaml?lnId={0}&misc={1}",
                                               (e.OriginalSource as Button).Name, (e.OriginalSource as Button).Tag);
            }
            else
            {
                tempUri = String.Format("/Pages/LineDetails.xaml?lnId={0}", (e.OriginalSource as Button).Name);
            }
            NavigationService.Navigate(new Uri(tempUri, UriKind.RelativeOrAbsolute));
        }

        #region Search handlers

        private void SearchInputBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.Focus();
        }

        private void SearchInputBox_KeyUp(object sender, KeyEventArgs e)
        {
            App.ViewModel.FilterText = this.SearchInputBox.Text;
            if (e.Key == Key.Enter)
            {
                SearchPanel.Opacity = 0;
                this.Focus();
            }
        }

        private void appbarSearchItem_Click(object sender, EventArgs e)
        {
            myPanorama.DefaultItem = myPanorama.Items[1];
            if (SearchPanel.Opacity == 0)
            {
                SearchPanel.Opacity = 100;
                SearchInputBox.Focus();
                SearchInputBox.SelectAll();
            }
            else
            {
                SearchPanel.Opacity = 0;
                this.Focus();
            }
        }

        #endregion

        private void MapPreview_KeyUp(object sender, KeyEventArgs e)
        {
            // TODO: open map in separate window
        }

        private void StationButton_Click(object sender, RoutedEventArgs e)
        {
            int stId = Int32.Parse((sender as Button).Name);
            StationItemViewModel st = App.ViewModel.Near.Items.First(i => i.stId == stId);
            App.ViewModel.Map.AddMarker(st.Latitude, st.Longitude);
        }

        private void btnAddRoute_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/AddRoute.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}